package com.lijiajia.cloud.resource.sys.config.security.resourceserver;

import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 资源服务器 bean配置
 *
 * @author lijiajia
 * @since 2019-05-28
 */

@Configuration
public class ResourceServerConfiguration {
    private final ResourceServerProperties resource;

    public ResourceServerConfiguration(ResourceServerProperties resource) {
        this.resource = resource;
    }

    // @Bean
    // public OAuth2WebSecurityExpressionHandler expressionHandler(ApplicationContext applicationContext) {
    //     OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
    //     expressionHandler.setApplicationContext(applicationContext);
    //     return expressionHandler;
    // }

    // @Bean
    // public JwtAccessTokenConverter accessTokenConverter() {
    //     JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    //     String keyValue = this.resource.getJwt().getKeyValue();
    //     if (!StringUtils.hasText(keyValue)) {
    //         keyValue = getKeyFromServer();
    //     }
    //     if (StringUtils.hasText(keyValue) && !keyValue.startsWith("-----BEGIN")) {
    //         converter.setSigningKey(keyValue);
    //     }
    //     if (keyValue != null) {
    //         converter.setVerifierKey(keyValue);
    //     }
    //     // if (!CollectionUtils.isEmpty(this.configurers)) {
    //     //     AnnotationAwareOrderComparator.sort(this.configurers);
    //     //     for (JwtAccessTokenConverterConfigurer configurer : this.configurers) {
    //     //         configurer.configure(converter);
    //     //     }
    //     // }
    //     return converter;
    // }

    /*private String getKeyFromServer() {
        RestTemplate keyUriRestTemplate = new RestTemplate();
        if (!CollectionUtils.isEmpty(this.customizers)) {
            for (JwtAccessTokenConverterRestTemplateCustomizer customizer : this.customizers) {
                customizer.customize(keyUriRestTemplate);
            }
        }
        HttpHeaders headers = new HttpHeaders();
        String username = this.resource.getClientId();
        String password = this.resource.getClientSecret();
        if (username != null && password != null) {
            byte[] token = Base64.getEncoder()
                    .encode((username + ":" + password).getBytes());
            headers.add("Authorization", "Basic " + new String(token));
        }
        HttpEntity<Void> request = new HttpEntity<>(headers);
        String url = this.resource.getJwt().getKeyUri();
        return (String) keyUriRestTemplate
                .exchange(url, HttpMethod.GET, request, Map.class).getBody()
                .get("value");
    }*/

    // @Bean
    // public ResourceServerTokenServices resourceServerTokenServices() {
    //     return null;
    // }
    //
    // @Bean
    // public TokenStore tokenStore() {
    //     TokenStore tokenStore = new JwtTokenStore();
    //     return y
    // }
}
